package com.gitee.feizns.explore.data_structure.tree.bs.mid;

import com.gitee.feizns.explore.data_structure.linked_list.singly.ListNode;

import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * @author feizns
 * @since 2020/3/19
 */
public class ListOfDepth {

    public static void main(String[] args) {

    }

    public static ListNode[] listOfDepth(TreeNode tree) {
        List<ListNode> ret = new LinkedList<>();
        Queue<TreeNode> queue = new LinkedList<>();
        if ( tree != null )
            queue.add(tree);
        while ( !queue.isEmpty() ) {
            ListNode dummy = new ListNode(0);
            ListNode tmp = dummy;
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                TreeNode node = queue.remove();
                tmp.next = new ListNode(node.val);
                tmp = tmp.next;
                if ( node.left != null )
                    queue.add(node.left);
                if ( node.right != null )
                    queue.add(node.right);
            }
            ret.add(dummy.next);
        }
        return ret.toArray(new ListNode[]{});
    }

}
